package com.java.control.impl;


import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;

import com.java.common.DateUtil;
import com.java.common.ProductFileService;
import com.java.common.QuotationFileService;
import com.java.common.SessionInfo;
import com.java.control.AbstractAdminFormController;
import com.java.entity.Product;
import com.java.entity.QIndex;
import com.java.entity.QList;
import com.java.entity.SupplierQuotation;
import com.java.entity.User;
import com.java.service.ProductService;
import com.java.service.QuotationService;
import com.java.service.SupplierQuotationService;
import com.java.service.SupplierService;
import com.java.service.UserService;

/**
 * 产品添加
 * @author wqf
 * @version  Nov 23, 2011 1:10:42 PM
 */
public class QListEditController extends AbstractAdminFormController{

	public QListEditController(){
		this.setCommandClass(QList.class);
	}
		
	protected void addSubmit(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors) throws Exception {
		QList qIndex = (QList)command;		
		if(qIndex.getQuotation()!=null){
			SupplierQuotation sq = this.supplierQuotationService.getSupplierQuotationById(qIndex.getQuotation().getId());
			if(sq!=null){
				if(sq.getProduct()!=null){
					qIndex.setProduct(sq.getProduct());
					File file = ProductFileService.Product.getFile(sq.getProduct().getImageUri());
					if(file.exists()){
						String path = QuotationFileService.Quotation.copy(file);
						qIndex.setImageUri(path);
					}
				}
				qIndex.setGrossWeight(sq.getGrossWeight());
				qIndex.setNetWeight(sq.getNetWeight());
			}			
		}
		this.quotationService.editQList(qIndex);
		request.setAttribute("operation", "add");
	}	
	
	protected void modifySubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{		
		QList qIndex = (QList)command;
				
		this.quotationService.editQList(qIndex);
		
		request.setAttribute("operation", "modify");
		request.setAttribute("id", qIndex.getId());
	}
		
	protected Object getCommandDataForModify(HttpServletRequest request){
		Long id=Long.valueOf(request.getParameter("id"));
		QIndex qIndex = this.quotationService.getQIndexById(id);		
		return qIndex;
	}
	
	protected Object getCommandDataForAdd(HttpServletRequest request) {
		QList qlist = new QList();
		String id = request.getParameter("qindex");
		if(StringUtils.isNotBlank(id)){
			QIndex qindex = this.quotationService.getQIndexById(Long.parseLong(id));
			qlist.setQid(qindex);
			qlist.setQuotation(new SupplierQuotation());
		}
		return qlist;
	}
			
	public QuotationService quotationService;
	public UserService userService;
	public ProductService productService;
	public SupplierQuotationService supplierQuotationService;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public QuotationService getQuotationService() {
		return quotationService;
	}

	public void setQuotationService(QuotationService quotationService) {
		this.quotationService = quotationService;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public SupplierQuotationService getSupplierQuotationService() {
		return supplierQuotationService;
	}

	public void setSupplierQuotationService(
			SupplierQuotationService supplierQuotationService) {
		this.supplierQuotationService = supplierQuotationService;
	}

	
}
